home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / util / moni / Scout-src.lha / source / objects / scout_timer.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-02-13  |  11.4 KB  |  329 lines

  1. /**
  2.  * Scout - The Amiga System Monitor
  3.  *
  4.  *------------------------------------------------------------------
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * You must not use this source code to gain profit of any kind!
  21.  *
  22.  *------------------------------------------------------------------
  23.  *
  24.  * @author Andreas Gelhausen
  25.  * @author Richard Körber <rkoerber@gmx.de>
  26.  */
  27.  
  28.  
  29.  
  30. #include "system_headers.h"
  31.  
  32. static APTR TimerPool = NULL;
  33.  
  34. __asm __saveds LONG timerlist_dspfunc(register __a2 char **array, register __a1 struct TimerEntry *timerentry, register __a0 struct Hook *hook)
  35. {
  36.    if (timerentry) {
  37.       *array++ = timerentry->timer_address;
  38.       *array++ = timerentry->timer_replyport;
  39.       *array++ = timerentry->timer_timeout;
  40.       *array++ = timerentry->timer_unit;
  41.       *array   = timerentry->timer_name;
  42.    } else {
  43.       *array++ = ESC "bAddress";
  44.       *array++ = ESC "bReplyPort";
  45.       *array++ = ESC "bTime";
  46.       *array++ = ESC "bUnit";
  47.       *array   = ESC "bTask";
  48.    }
  49.    return(0);
  50. }
  51.  
  52. struct Hook timerlist_dsphook = {
  53.  {NULL, NULL},
  54.  (ULONG (* )())timerlist_dspfunc,
  55.  NULL, NULL
  56. };
  57.  
  58. __asm __saveds LONG timerlist_cmpfunc(register __a1 struct TimerEntry *timerentry1, register __a2 struct TimerEntry *timerentry2)
  59. {
  60.    LONG cmp;
  61.  
  62.    cmp = strcmpi(timerentry1->timer_name, timerentry2->timer_name);
  63.    if (cmp == 0) cmp = strcmpi(timerentry1->timer_address, timerentry2->timer_address);
  64.  
  65.    return cmp;
  66. }
  67.  
  68. struct Hook timerlist_cmphook = {
  69.  {NULL, NULL},
  70.  (ULONG (* )())timerlist_cmpfunc,
  71.  NULL, NULL
  72. };
  73.  
  74. void FreeTimer (void)
  75. {
  76.     MyFreePoolStructs(&TimerPool, timertext, NULL, timerlist);
  77. }
  78.  
  79. int GetTimer (struct TimerEntry **first) {
  80.    struct   timerequest *tr;
  81.    struct   timerequest *search;
  82.    struct   MsgPort *mp;
  83.    struct   TimerEntry  *timerentry,*previous = NULL;
  84.    // char     code[FILENAMELENGTH + 1];
  85.    ULONG    time, eclk, std, min, sec;
  86.    struct   EClockVal eclock;
  87.  
  88.    int timercnt = 0;
  89.    *first = 0;
  90.  
  91.    if (!TimerPool) TimerPool = tbCreatePool(MEMF_CLEAR, 4096, 4096);
  92.  
  93.    if (clientstate) {
  94.       if (SendDaemon ("GetTimerList")) {
  95.          while ((timerentry = tbAllocPooled(TimerPool, sizeof(struct TimerEntry))) \
  96.            && (ReceiveDecodedEntry ((UBYTE *) timerentry, sizeof (struct TimerEntry)))) {
  97.             IsHex (timerentry->timer_address, (long *) &timerentry->timer_adr);
  98.  
  99.             if (! *first)
  100.                *first = timerentry;
  101.             if (previous)
  102.                previous->timer_next = timerentry;
  103.  
  104.             timercnt++;
  105.             previous = timerentry;
  106.          }
  107.       }
  108.    } else {
  109.       if (mp = CreateMsgPort()) {
  110.          if (tr = CreateIORequest(mp,sizeof(struct timerequest))) {
  111.             if (!OpenDevice("timer.device",UNIT_MICROHZ,(struct IORequest *)tr,0)) {
  112.                TimerBase = tr->tr_node.io_Device;
  113.                tr->tr_node.io_Command = TR_ADDREQUEST;
  114.                tr->tr_time.tv_secs = 0;
  115.                tr->tr_time.tv_micro = 10000;    /* 10ms */
  116.                Forbid();
  117.                SendIO((struct IORequest *)tr);
  118.                for(search = tr;
  119.                    search->tr_node.io_Message.mn_Node.ln_Pred;
  120.                    search = (struct timerequest *)search->tr_node.io_Message.mn_Node.ln_Pred);
  121.  
  122.                eclk = ReadEClock(&eclock);
  123.                eclk /= 100;
  124.  
  125.                search = (struct timerequest *) search->tr_node.io_Message.mn_Node.ln_Succ;
  126.                while (search->tr_node.io_Message.mn_Node.ln_Succ != 0) {
  127.                  if (search!=tr) {
  128.                      if (!(timerentry = tbAllocPooled(TimerPool, sizeof(struct TimerEntry)))) break;
  129.                      if (! *first)
  130.                         *first = timerentry;
  131.                      if (previous)
  132.                         previous->timer_next = timerentry;
  133.  
  134.                      time = search->tr_time.tv_micro;
  135.                      if(time > eclock.ev_lo)
  136.                         time -= eclock.ev_lo;
  137.                      else
  138.                         time = 0;
  139.                      time /= eclk;
  140.  
  141.                      sec = search->tr_time.tv_secs + (time/100);
  142.                      min = sec/60; sec %= 60;
  143.                      std = min/60; min %= 60;
  144.  
  145.                      timerentry->timer_adr = search;
  146.                      _sprintf (timerentry->timer_address, "$%08lx", search);
  147.                      // strncpy (timerentry->timer_name, nonetest (((struct Node *)search->tr_node.io_Message.mn_ReplyPort->mp_SigTask)->ln_Name), 27);
  148.                      strncpy (timerentry->timer_name, nonetest(GetTaskName(search->tr_node.io_Message.mn_ReplyPort->mp_SigTask)), 27);
  149.                      _sprintf (timerentry->timer_replyport, "$%08lx", search->tr_node.io_Message.mn_ReplyPort);
  150.                      strcpy  (timerentry->timer_unit, "MicroHz");
  151.                      _sprintf (timerentry->timer_timeout, "%ld:%02ld'%02ld.%02ld\"", std, min, sec, time%100);
  152.                      timercnt++;
  153.  
  154.                      previous = timerentry;
  155.                   }
  156.                   search = (struct timerequest *) search->tr_node.io_Message.mn_Node.ln_Succ;
  157.                }
  158.                Permit();
  159.                WaitIO((struct IORequest *)tr);
  160.                CloseDevice((struct IORequest *)tr);
  161.             }
  162.             if (!OpenDevice("timer.device",UNIT_VBLANK,(struct IORequest *)tr,0)) {
  163.                TimerBase = tr->tr_node.io_Device;
  164.                tr->tr_node.io_Command = TR_ADDREQUEST;
  165.                tr->tr_time.tv_secs = 0;
  166.                tr->tr_time.tv_micro = 20000;    /* 20ms */
  167.                Forbid();
  168.                SendIO((struct IORequest *)tr);
  169.                for(search = tr;
  170.                    search->tr_node.io_Message.mn_Node.ln_Pred;
  171.                    search = (struct timerequest *)search->tr_node.io_Message.mn_Node.ln_Pred);
  172.  
  173.                eclk = ReadEClock(&eclock);
  174.  
  175.                search = (struct timerequest *) search->tr_node.io_Message.mn_Node.ln_Succ;
  176.                while (search->tr_node.io_Message.mn_Node.ln_Succ != 0) {
  177.                  if (search!=tr) {
  178.                      if (!(timerentry = tbAllocPooled(TimerPool, sizeof(struct TimerEntry)))) break;
  179.                      if (! *first)
  180.                         *first = timerentry;
  181.                      if (previous)
  182.                         previous->timer_next = timerentry;
  183.  
  184.                      time = search->tr_time.tv_micro;
  185.  
  186.                      if(time > eclock.ev_lo)
  187.                         time = ((time-eclock.ev_lo)*100)/eclk;
  188.                      else
  189.                         time = 0;
  190.  
  191.                      sec = search->tr_time.tv_secs + (time/100);
  192.                      min = sec/60; sec %= 60;
  193.                      std = min/60; min %= 60;
  194.  
  195.                      timerentry->timer_adr = search;
  196.                      _sprintf (timerentry->timer_address, "$%08lx", search);
  197.                      // strncpy (timerentry->timer_name, nonetest (((struct Node *)search->tr_node.io_Message.mn_ReplyPort->mp_SigTask)->ln_Name), 27);
  198.                      strncpy (timerentry->timer_name, GetTaskName(search->tr_node.io_Message.mn_ReplyPort->mp_SigTask), 27);
  199.                      _sprintf (timerentry->timer_replyport, "$%08lx", search->tr_node.io_Message.mn_ReplyPort);
  200.                      strcpy  (timerentry->timer_unit, "VBlank");
  201.                      _sprintf (timerentry->timer_timeout, "%ld:%02ld'%02ld.%02ld\"", std, min, sec, time%100);
  202.                      timercnt++;
  203.  
  204.                      previous = timerentry;
  205.                   }
  206.                   search = (struct timerequest *) search->tr_node.io_Message.mn_Node.ln_Succ;
  207.                }
  208.                Permit();
  209.                WaitIO((struct IORequest *)tr);
  210.                CloseDevice((struct IORequest *)tr);
  211.             }
  212.             DeleteIORequest((struct IORequest *)tr);
  213.          }
  214.          DeleteMsgPort(mp);
  215.       }
  216.    }
  217.    return (timercnt);
  218. }
  219.  
  220. void PrintTimer (char *filename) {
  221.    int   i=1;
  222.    BPTR  handle;
  223.    struct TimerEntry *entryp = NULL;
  224.  
  225.    handle = HandlePrintStart (filename);
  226.    if ((handle) && (PrintOneLine (handle, "\n  Address  ReplyPort    Time       Unit    Name\n\n"))) {
  227.       if (! WI_Timer) {
  228.          i = GetTimer (&entryp);
  229.       }
  230.       if (i) {
  231.          for (i=0;;i++) {
  232.             if (WI_Timer)
  233.                DoMethod (timerlist,MUIM_List_GetEntry,i,&entryp);
  234.             if (!entryp) break;
  235.  
  236.             _sprintf (tmpstr2, " %s %s %13s %7s %s\n", entryp->timer_address, entryp->timer_replyport, entryp->timer_timeout, entryp->timer_unit, entryp->timer_name);
  237.             if (! (PrintOneLine (handle, tmpstr2)))
  238.                break;
  239.  
  240.             if (! WI_Timer)
  241.                entryp = entryp->timer_next;
  242.          }
  243.       }
  244.    }
  245.    HandlePrintStop();
  246. }
  247.  
  248. void ShowTimer (void) {
  249.    struct TimerEntry *timer;
  250.  
  251.    ApplicationSleep();
  252.    set (timerlist,MUIA_List_Quiet,TRUE);
  253. //*   set (BT_TimerRemove, MUIA_Disabled, TRUE);
  254.  
  255.    FreeTimer();
  256.    timercnt = GetTimer (&timer);
  257.  
  258.    while (timer) {
  259.       InsertSortedEntry (timerlist, (APTR *) &timer);
  260.       timer = timer->timer_next;
  261.    }
  262.  
  263.    DoMethod(timerlist,MUIM_List_Sort);
  264.  
  265.    SetCountText (timercount, timercnt);
  266.    AwakeApplication();
  267.    set (timerlist,MUIA_List_Quiet,FALSE);
  268. }
  269.  
  270. void SendTimerList (void) {
  271.    struct TimerEntry *timer;
  272.  
  273.    FreeTimer();
  274.    timercnt = GetTimer (&timer);
  275.  
  276.    while (timer) {
  277.       SendEncodedEntry ((UBYTE *) timer, sizeof (struct TimerEntry));
  278.       timer = timer->timer_next;
  279.    }
  280.    FreeTimer();
  281. }
  282.  
  283.  
  284. char timer_title[WINDOWTITLELEN];
  285.  
  286. void TimerWindow (BOOL state) {
  287.    if (state) {
  288.       if (WI_Timer) {
  289.          ShowTimer();
  290.       } else {
  291.          WI_Timer = WindowObject,
  292.          MUIA_Window_Title, MyGetWindowTitle (timer_title, "TIMER REQUESTS"),
  293.          MUIA_HelpNode, TimerText,
  294.          MUIA_Window_ID, MakeListID('T','I','M','R'),
  295.          WindowContents, VGroup,
  296.             Child, timerlist = MySortedListviewObject ("DELTA=8,DELTA=8,DELTA=8 P=\33r,DELTA=8 P=\33c,",&timerlist_dsphook, &timerlist_cmphook),
  297.             Child, MyBelowListview (&timertext, &timercount),
  298.             Child, MyVSpace(2),
  299.             Child, HGroup, MUIA_Group_SameSize, TRUE,
  300.                Child, BT_TimerUpdate    = KeyButtonA (UpdateText  ,ID_TIMERUPDATE),
  301.                Child, BT_TimerPrint     = KeyButtonA (PrintText   ,ID_TIMERPRINT),
  302. //*               Child, BT_TimerRemove    = KeyButtonA (RemoveText  ,ID_TIMERREMOVE),
  303.                Child, BT_TimerExit      = KeyButtonA (ExitText    ,ID_TIMEREXIT),
  304.             End,
  305.          End, End;
  306.  
  307.          DoMethod (AP_Scout,OM_ADDMEMBER,WI_Timer);
  308.          DoMethod (WI_Timer,MUIM_Window_SetCycleChain,timerlist,BT_TimerUpdate,BT_TimerPrint,BT_TimerExit,NULL);
  309.  
  310.          SetCloseRequest (WI_Timer,ID_TIMEREXIT);
  311.          SetListActive (timerlist,ID_TIMERLV_ACTIVE);
  312.  
  313.          ShowTimer();
  314.  
  315.          SetWindowOpen (WI_Timer,timerlist,ID_TIMEREXIT);
  316.       }
  317.    } else if ((! state) && (WI_Timer)) {
  318.       SetWindowClose (WI_Timer,TRUE);
  319.  
  320.       FreeTimer();
  321.  
  322.       DoMethod (AP_Scout,OM_REMMEMBER,WI_Timer);
  323.       MUI_DisposeObject (WI_Timer);
  324.       WI_Timer = NULL;
  325.       timerlist = NULL;
  326.    }
  327. }
  328.  
  329.